﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SqlMiM.Common.Interfaces;
using System.ComponentModel;
using SqlMiM.Common.Themes;

namespace SqlMiM.Common.Framework.Options
{
    public class OptionsService : INotifyPropertyChanged
    {
        #region Singleton

        private static OptionsService current;

        public static OptionsService Current
        {
            get
            {
                if (current == null)
                {
                    current = new OptionsService();
                }

                return current;
            }
        }

        #endregion

        // Fields
        private IOptionsPage activePage;
        private List<IOptionsPage> optionsPages;

        // Events
        public event PropertyChangedEventHandler PropertyChanged;

        // Properties
        public IOptionsPage ActivePage
        {
            get
            {
                if (this.activePage != null)
                    return this.activePage;

                if (this.OptionsPages.Count > 0)
                    return this.OptionsPages[0];

                return null;
            }
            set
            {
                this.activePage = value;
                this.RaisePropertyChanged("ActivePage");
            }
        }

        public List<IOptionsPage> OptionsPages
        {
            get
            {
                return this.optionsPages;
            }
        }

        // Methods
        public OptionsService()
        {
         //   this.configurationService = configurationService;
            this.optionsPages = new List<IOptionsPage>();
        }

        /// <summary>
        /// Add an OptionPage
        /// </summary>
        public void Add(IOptionsPage optionPage)
        {
            this.OptionsPages.Add(optionPage);
        }


        public void Cancel()
        {
            if (this.OptionsPages.Count == 0) return;

            foreach (IOptionsPage page in this.OptionsPages)
            {
                page.Cancel();
            }
        }

        public void Commit()
        {
            if (this.OptionsPages.Count == 0) return;
            
            foreach (IOptionsPage page in this.OptionsPages)
            {
                page.Commit();
            }
            Context.Current.Configuration.Save();
        }

        private void Remove(IOptionsPage value)
        {
            this.OptionsPages.Remove(value);
        }

        private void RaisePropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }


    }


}
